package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;
    //将预约数据保存到数据库
    @Override
    public void importOrderSettings(ArrayList<OrderSetting> orderSettings) {
        for (OrderSetting orderSetting : orderSettings) {
            //检查当前预约日期是否在数据库中设置过
            Integer count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
            if (count>0) {
                //已存在
                orderSettingMapper.updateNumberByOrderDate(orderSetting);
            } else {
                //未存在
                orderSettingMapper.add(orderSetting);
            }
        }


    }

    //查询预约设置信息
    @Override
    public List<Map> findOrderSettingByMonth(String date) {
        //定义月份第一天
        String dateBegin = date+"-1";//2019-7-1
        //定义月份的最后一天
        String dateEnd = date+"-31";//2019-7-31

        //同当前月份的第一天和最后一天查询当前月所有的预约设置数据
        List<OrderSetting> orderSettings = orderSettingMapper.findOrderSettingByMonth(dateBegin,dateEnd);

        List list = new ArrayList();

        //遍历每一个OrderSetting，将数据封装到Map中
        for (OrderSetting orderSetting : orderSettings) {
            Map map = new HashMap<>();
            map.put("date",orderSetting.getOrderDate().getDate());//获取几号
            map.put("number",orderSetting.getNumber());
            map.put("reservations",orderSetting.getReservations());

            //将所有map封装到list中
            list.add(map);
        }

        return list;
    }

    //设置可预约数量
    @Override
    public void updateNumberByOrderDate(OrderSetting orderSetting) {
        //根据日期统计之前是否设置过预约数据
        int count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
        if (count>0){
            //如果设置过预约数据，执行更新
            orderSettingMapper.updateNumberByOrderDate(orderSetting);
        }else{
            //如果之前没有设置过预约数据，执行新增
            orderSettingMapper.add(orderSetting);
        }
    }
}
